package com.gitee.aurora.service;


import com.balloon.springboot.datasource.aop.DataSourceRouteKey;
import com.gitee.aurora.comm.AuroraJdbcTemplate;
import com.gitee.aurora.entity.PtConfig;
import com.gitee.aurora.entity.PtcjConfig;
import com.gitee.aurora.entity.SqlConfigItem;
import com.gitee.aurora.entity.TableConfig;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PtConfigService {


    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;
    @DataSourceRouteKey(type="defaultMysqlDB")
    public  int insertPtConfigService(PtConfig ptConfig){

        String sql = "insert into  TAB_PT_CONFIG (PT_XL_TYPE,PT_XL_TYPE_NAME,PT_SQL,PT_TABLE,PT_CODE,PT_CODE_NAME,S_CODE) values (?,?,?,?,?,?,?)";
        //int[] types = new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR,Types.VARCHAR, Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.VARCHAR};
        int result= auroraJdbcTemplate.update(sql, new Object[]{ptConfig.getPtXlType(),ptConfig.getPtXlTypeName(),ptConfig.getPtSql(),ptConfig.getPtTable(),ptConfig.getPtCode(),ptConfig.getPtCodeName(),ptConfig.getSCode()});
        return  result;

    }
    @DataSourceRouteKey(type="defaultMysqlDB")
    public  int deletePtConfigService(PtConfig ptConfig){

        String sql = "delete  from  TAB_PT_CONFIG  where  S_ID=?";
        //int[] types = new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR,Types.VARCHAR, Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.VARCHAR};
        int result= auroraJdbcTemplate.update(sql, new Object[]{ptConfig.getSId()});
        return  result;

    }
    @DataSourceRouteKey(type="defaultMysqlDB")
    public  int updatePtConfigService(PtConfig ptConfig){
        List<Object> params = new ArrayList<>();
        String sql = "update TAB_PT_CONFIG  set PT_XL_TYPE=? ,PT_XL_TYPE_NAME=?,PT_SQL=?,PT_TABLE=?, PT_CODE=?,PT_CODE_NAME=?,S_CODE=?   where  S_ID=?";
        params.add(ptConfig.getPtXlType());
        params.add(ptConfig.getPtXlTypeName());
        params.add(ptConfig.getPtSql());
        params.add(ptConfig.getPtTable());
        params.add(ptConfig.getPtCode());
        params.add(ptConfig.getPtCodeName());
        params.add(ptConfig.getSCode());
        params.add(ptConfig.getSId());
        //int[] type    s = new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR,Types.VARCHAR, Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.VARCHAR};
        int result= auroraJdbcTemplate.update(sql, params.toArray());
        return  result;

    }

    //查询平台集合
    @DataSourceRouteKey(type="defaultMysqlDB")
    public Pagination queryPtConfigService(PtConfig ptConfig, PageParam pageParam) throws Exception {

        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select S_ID AS sId, PT_XL_TYPE as ptXlType ,PT_XL_TYPE_NAME as ptXlTypeName,PT_SQL as ptSql,PT_TABLE as ptTable,DATE_FORMAT(DT_CREATE_TIME,'%Y-%m-%d %H:%i:%s') as dtCreateTime" +
                ",DATE_FORMAT(DT_UPDATE_TIME,'%Y-%m-%d %H:%i:%s') as  dtUpdateTime ,PT_CODE as ptCode ,PT_CODE_NAME as ptCodeName ,S_CODE as sCode from TAB_PT_CONFIG where 1=1 ");
        int[] types = new int[]{java.sql.Types.INTEGER, java.sql.Types.INTEGER, Types.VARCHAR};
        if(ptConfig!=null){
            if(ptConfig.getPtXlType()!=null){
                sql.append("and  PT_XL_TYPE = ?");
                params.add(ptConfig.getPtXlType());
            }
            if(ptConfig.getPtCode()!=null){
                sql.append("and  PT_CODE= ?");
                params.add(ptConfig.getPtCode());
            }
            if(ptConfig.getSCode()!=null){
                sql.append("and  S_CODE = ?");
                params.add(ptConfig.getSCode());
            }
        }
        return  new Pagination(sql.toString(),params.toArray(),pageParam,auroraJdbcTemplate);
    }
    //查询平台类型.
    @DataSourceRouteKey(type="defaultMysqlDB")
    public List<SqlConfigItem> queryPtLx() throws Exception {
        String sql = "SELECT  DICT_CODE AS dictCode ,dictCodeName FROM TAB_SQL_CONFIG_ITEM   GROUP BY DICT_CODE ,DICT_CODE_NAME";
        List<SqlConfigItem> pList = auroraJdbcTemplate.queryForMapToList(sql,SqlConfigItem.class);
        return  pList;
    }
    //查询平台系列类型
    @DataSourceRouteKey(type="defaultMysqlDB")
    public List<SqlConfigItem> queryPtXl() throws Exception {
        String sql = "SELECT TYPE_CODE  as typeCode,TYPE_NAME as typeName FROM TAB_SQL_CONFIG_ITEM   GROUP BY TYPE_CODE ,TYPE_NAME ";
        List<SqlConfigItem> pList = auroraJdbcTemplate.queryForMapToList(sql, SqlConfigItem.class);
        return  pList;
    }
    //查询所有检测表
    @DataSourceRouteKey(type="defaultMysqlDB")
    public List<TableConfig> queryTable() throws Exception {

        String sql = "select TABLE_CODE as tableCode,TABLE_CODE_NAME AS tableCodeName from TAB_TABLE_CONFIG";
        List<TableConfig> pList = auroraJdbcTemplate.queryForMapToList(sql,TableConfig.class);
        return  pList;
    }
    //查询所有监测机构
    @DataSourceRouteKey(type="defaultMysqlDB")
    public List<PtcjConfig>  queryPtcjConfig() throws Exception {

        String sql = "SELECT\n" +
                "\tg.S_ID AS sId,\n" +
                "\tg.S_CODE AS sCode,\n" +
                "\tg.S_CODE_NAME AS sCodeName,\n" +
                "\tg.PT_CJ AS ptCj,\n" +
                "\tg.PT_IP AS ptIp,\n" +
                "\tg.F_CODE AS fCode,\n" +
                "\tg.F_CODE_NAME AS fCodeName,\n" +
                "\tg.F_CODE_ORDER AS fCodeOrder,\n" +
                "  t.PT_SQL AS ptSql,\n" +
                "  t.PT_TABLE AS ptTable,t.PT_CODE AS ptCode\n" +
                "FROM\n" +
                "\tTAB_PTCJ_CONFIG  g LEFT \n" +
                "JOIN TAB_PT_CONFIG  t on g.S_CODE=t.S_CODE";
        List<PtcjConfig> pList = auroraJdbcTemplate.queryForMapToList(sql,PtcjConfig.class);

        List<PtcjConfig> collect = pList.stream().filter(node -> node.getFCode()==null)
                .map(tree -> {
                    tree.setChildList(getChildList(tree, pList));
                    return tree;
                }) .collect(Collectors.toList());
        return  collect;
    }

    private List<PtcjConfig> getChildList(PtcjConfig root, List<PtcjConfig> all) {
        List<PtcjConfig> collect = all.stream().filter(node -> {
            return Objects.equals(node.getFCode(), root.getSCode());
        }).map(node -> {
            node.setChildList(getChildList(node, all));
            return node;
        }).collect(Collectors.toList());
        return collect;
    }



}



